package k8.buffer;

import k8.buffer.var.VARIndicesBuffer;
import k8.buffer.vbo.VBOIndicesBuffer;

import org.lwjgl.opengl.GLContext;


public abstract class IndicesBuffer extends Buffer {
    
    /**
     * Creates an instance of IndicesBuffer.
     * 
     * @param initialcapacity
     *          Initial capacity (number of vertices)
     */
    public IndicesBuffer(int initialcapacity) {
        super(initialcapacity);
    }
    
    /**
     * Gets an instance of IndicesBuffer.
     * 
     * @param initialcapacity
     *          Initial capacity (number of indices)
     */
    public static IndicesBuffer getInstance(int initialcapacity) {
        return ( GLContext.getCapabilities().GL_ARB_vertex_buffer_object ) ? new VBOIndicesBuffer(initialcapacity) : new VARIndicesBuffer(initialcapacity);
    }
    
    /** Adds the next three successive indices to the buffer */
    public void increment() {
    	int elements = getElements();
    	// Ensure another 12 bytes is available, check for growth, assign next 3
    	if ( getSlice(12) == null ) {
    		buffer.limit((elements+3)*4);
    		elements = 0;
    	}
    	this.elements = elements;
    	while ( buffer.hasRemaining() )	buffer.putInt(this.elements++);
    	flush();
    }
    
    /** Tells OpenGL to render, using this buffer as the indices */
    public abstract void glDrawElements();

}
